<script setup lang="ts">
    import { reactive, ref, onMounted } from 'vue'//引入vue函数
    import type { User } from '@/model' //导入自定义数据类型
    import UserEdit from './components/UserEdit.vue' //修改用户页面
    import { getUserList, changeState, deleteUser } from '@/api' //数据接口
    import { ElMessage, ElMessageBox } from 'element-plus' //导入消息框

    //表格数据对象
    const tableData = ref<User[]>([])
    //表格原始数据
    const oldData = ref<User[]>([])

    //是否显示 添加修改用户对话框
    const isShowDialog =  ref(false)

    //添加修改用户对话框的标题
    const dialogTitle = ref("新增用户")

    //定义表单数据对象
    const searchForm = reactive({
        username:"",
        state:"0"
    });

    //当前将要修改的用户
    const user = reactive({
        id:0,
        username:"",
        password:""
    })

    //搜索用户
    function searchUser(){
        if(searchForm.username == "" && searchForm.state == "0"){
            //查询所有用户
            loadUserList()
        }
        else{
            //对现有用户进行过滤
            tableData.value = oldData.value.filter((item:User)=>{
                if(searchForm.state == "0"){
                    return item.username?.includes(searchForm.username)
                }
                else{
                    return item.username?.includes(searchForm.username) && item.state==(searchForm.state=="1")?true:false
                }
            })
        }
    }

    //重置搜索表单
    function resetForm(){
        searchForm.username = ""
        searchForm.state = "0"
        tableData.value = oldData.value
    }

    //新增用户
    function addUser(){
        dialogTitle.value = "新增用户"
        user.id = 0
        user.username = ""
        user.password = ""
        isShowDialog.value = true
    }

    //修改用户
    function updateUser(data:User){
        dialogTitle.value = "修改用户"
        Object.assign(user, data)
        isShowDialog.value = true
    }

    //删除用户
    function delUser(data:User){
        //确认对话框
        ElMessageBox.confirm("确定要删除此用户吗？", {
            closeOnClickModal: true,
            confirmButtonText: "确定",
            cancelButtonText: "取消",
        }).then(async ()=>{
            const res = await deleteUser({id:data.id})
            if(res.data.errno == 0){
                //重置搜索表单
                resetForm()
                //重新加载数据
                loadUserList()
                //删除成功
                ElMessage.success(res.data.errmsg)
            }
            else{
                //删除失败
                ElMessage.error(res.data.errmsg)
            }
        }).catch(()=>{
            //console.log("点击了取消按扭")
        })
    }

    //查询所有用户数据
    async function loadUserList(){
        const res = await getUserList()
        oldData.value = res.data.data
        tableData.value = res.data.data
    }

    //更改用户状态
    async function switchState(data:User){
        const res = await changeState(data)
        if(res.data.errno == 0){
            //添加或修改成功
            ElMessage.success(res.data.errmsg)
        }
        else{
            //添加或修改失败
            ElMessage.error(res.data.errmsg)
        }
    }

    //关闭对话框
    function closeDialog(){
        isShowDialog.value = false
        loadUserList()
    }

    //组件加载完成
    onMounted(()=>{
        loadUserList()
    })

</script>

<template>

    <!--搜索表单-->
    <section class="search">
        <el-form :inline="true">
            <el-form-item label="登录名称">
                <el-input v-model="searchForm.username" style="width:200px;"></el-input>
            </el-form-item>
            <el-form-item label="用户状态">
                <el-select v-model="searchForm.state" style="width:200px;">
                    <el-option label="所有" value="0"></el-option>
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchUser">搜索</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </section>

    <!--表格显示内容-->
    <section class="user_page">
        <div><el-button type="primary" @click="addUser">新增用户</el-button></div>
        <div>
            <!--表格-->
            <el-table :data="tableData" row-key="id" style="width:100%" border>
                <!--空数据时显示内容-->
                <template #empty>暂无数据</template>
                <!--表格有数据时的显示内容-->
                <el-table-column prop="id" label="用户ID"></el-table-column>
                <el-table-column prop="username" label="用户名称"></el-table-column>
                <el-table-column label="用户状态" width="100">
                    <template #default="scope">
                        <el-switch v-model="scope.row.state" @change="switchState(scope.row)" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template #default="scope">
                        <el-button type="warning" @click="updateUser(scope.row)">编辑</el-button>
                        <el-button type="danger" @click="delUser(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </section>

    <!--新增分类、修改分类，对话框-->
    <el-dialog v-model="isShowDialog" :title="dialogTitle" width="600">
        <UserEdit :is-show="isShowDialog" :user="user" @closeDialog="closeDialog"></UserEdit>
    </el-dialog>

</template>

<style scoped>
    .user_page, .search{
        background:#FFFFFF;
        border-radius: 5px;
        box-shadow: 0px 0px 5px 5px #E4E7ED;
        margin:20px;
        border:1px solid transparent;
    }
    .search{
        height:50px;
        padding-left:20px;
        display:flex;
        align-items: center;
    }
    .search .el-form-item{
        margin-top:0px;
        margin-bottom:0px;
    }
    .user_page > div{
        margin:20px;
    }
    .el-switch{
        --el-switch-off-color: #ff4949
    }
</style>
